# 导入:
# import mysql.connector.errorcode
import sqlalchemy
from sqlalchemy import Column, String, Integer, DateTime, Boolean
from utils.sql.db import session, Base
from utils.sql.app.user import User
import time
from utils.sql.public.location import phone_location


# 定义User对象:
class Index(Base):
    # 表的名字:
    __tablename__ = 'index'

    # 表的结构:
    id = Column(Integer(), primary_key=True, default=None)
    icon = Column(String(20), default='')
    name = Column(String(255), default='')
    isTabBar = Column(Boolean)
    link = Column(String(255), default='')
    isShow = Column(Boolean)
    create_time = Column(String(20), default=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


# 只获取显示列表
def indexs():
    try:
        index = session.query(Index).filter(Index.isShow == 1).all()
        return {
            "code": 200,
            "data": index,
            "message": "查询成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "查询失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


# 获取所有列表
def pcindexs(query, pageNum, pageSize):
    try:
        index = session.query(Index).order_by(Index.id.desc())
        start = (int(pageNum) - 1) * int(pageSize)
        if query:
            index = index.filter(Index.name.like(f'%{query}%'))
        lists = index.limit(pageSize).offset(start).all()
        return {
            "code": 200,
            "data": {
                "list": lists,
                "current": pageNum,
                "total": index.count()
            },
            "message": "查询成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "查询失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


# 获取单个菜单
def pcindexOne(menuid):
    try:
        index = session.query(Index).filter(Index.id == menuid).one()
        return {
            "code": 200,
            "data": index,
            "message": "查询成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "查询失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


# 修改单个菜单
def updatemenu(data):
    try:
        index = session.query(Index).filter(Index.id == data.id).one()
        index.isShow = data.isShow
        index.icon = data.icon
        index.link = data.link
        index.name = data.name
        index.isTabBar = data.isTabBar
        # 提交即保存到数据库:
        session.commit()
        return {
            "code": 200,
            "data": index,
            "message": "修改成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "修改失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


# 新增菜单
def newmenu(data):
    try:
        # 创建新Feedback对象:
        new_menu = Index(
            icon=data.icon,
            isShow=data.isShow,
            isTabBar=data.isTabBar,
            link=data.link,
            name=data.name,
            create_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        )

        # 添加到session:
        session.add(new_menu)
        # 提交即保存到数据库:
        session.commit()
        return {
            "code": 200,
            "data": '',
            "message": "操作成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "操作失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


# 删除菜单
def deletemenu(menuid):
    try:
        delete_query = session.query(Index).filter(Index.id == menuid)
        # 执行删除操作
        delete_query.delete()

        # 提交即保存到数据库:
        session.commit()
        return {
            "code": 200,
            "data": '',
            "message": "操作成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "操作失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()
